<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\RentClothAfter\ConfirmOrder;
use App\Admin\Extensions\RentClothAfter\Refund;
use App\Models\RentClothAfter;
use App\Models\CommodityAttr;
use App\Models\ShopCom;
use App\Models\Wallet;
use App\Models\DealLog;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Widgets\Table;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class RentClothAfterController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '售后订单';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new RentClothAfter);

        $grid->column('id', __('Id'));
        $grid->user()->nickname('用户名');
        // $grid->column('rent_id', __('Rent id'));
        $grid->shop()->shop_name(__('店铺名'));
        // $grid->column('commodity_attr_id', __('商品名'));
        $grid->column('price', __('退款金额'));
        $grid->column('comment', __('备注'))->expand(function () {
            $attr_id = json_decode($this->commodity_attr_id);
            $shop_id = $this->shop_id;
            $com_lists = CommodityAttr::whereIn('id', $attr_id)->with('commodity')->get();
            $com_lists->transform(function ($item,$key) use ($shop_id) {
                $item->key = $key+1;
                $item->commodity_attr_id = $item->id;
                // $item->com_img = config('app.img_url') . ($item->commodity->pictures)[0];
                $item->com_name = $item->commodity->commodity_name;
                $item->com_desc = $item->commodity->commodity_desc;
                $item->com_price = ShopCom::where(['shop_id' => $shop_id, 'commodity_attr_id' => $item->id, 'is_buy' => 1])->value('price');
                $item->attr = $item->attr_value;
                return $item->only(['key','com_name', 'attr','com_desc', 'com_price']);
            });
      
            return new Table(['ID', '商品名称', '商品属性','商品描述', '商品价格'], $com_lists->toArray());
        });
        $grid->column('status', __('状态'))->display(function ($value) {
            switch ($value) {
                case '11':
                    return "<span class='label label-danger'>退货中</span>";
                    break;
                case '12':
                    return "<span class='label label-primary'>审核中</span>";
                    break;
                case '13':
                    return "<span class='label label-success'>已退款</span>";
                    break;
            }
        });
        $grid->column('created_at', __('申请时间'));
        $grid->filter(function ($filter) {
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            $filter->scope('退货中')->where('status', 11);
            $filter->scope('审核中')->where('status', 12);
            $filter->scope('已退款')->where('status', 13);

            $filter->between('created_at', '申请时间')->datetime();
        });

        $grid->actions(function ($actions) {
            $actions->disableView();
            $actions->disableEdit();
            $actions->disableDelete();
            if ($actions->row->status == 11) {
                $actions->append(new ConfirmOrder($actions->getKey())); //收货
            }
            if ($actions->row->status == 12) {
                $actions->append(new Refund($actions->getKey())); //退款
            }
            // $actions->append("<a href='/admin/rentClothAfterDetail?rent_id=" . $actions->getKey() . "'>" . '<i class="fa fa-sort-amount-asc"></i>' . "详情</a>");
        });
        return $grid;
    }
    public function confirmOrder(Request $request)
    {
        $id = $request->id;
        $rent_data = RentClothAfter::where('id', $id)->first();
        $rent_data->status = 12;

        DB::beginTransaction();
        try {
            $rent_data->save();
            DB::commit();
            admin_success('收货成功', '成功.');

        } catch (\Exception $exception) {

            DB::rollBack();
            admin_error('收货失败', '失败.');
        }
    }
    public function Refund(Request $request)
    {
        $id = $request->id;
        $rent_data = RentClothAfter::where('id', $id)->first();
        $rent_data->status = 13;
        $wallet = Wallet::where('user_id', $rent_data->user_id)->first();
        $wallet->money = bcadd($wallet->money, $rent_data->price, 2);
        DB::beginTransaction();
        try {
            $rent_data->save();
            $wallet->save();
            DealLog::add_log($rent_data->user_id, 0, $rent_data->price, 1, '售后退款');
            DB::commit();
            admin_success('退款成功', '成功.');

        } catch (\Exception $exception) {

            DB::rollBack();
            admin_error('退款失败', '失败.');
        }
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(RentClothAfter::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('user_id', __('User id'));
        $show->field('rent_id', __('Rent id'));
        $show->field('shop_id', __('Shop id'));
        $show->field('commodity_attr_id', __('Commodity attr id'));
        $show->field('price', __('Price'));
        $show->field('comment', __('Comment'));
        $show->field('status', __('Status'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new RentClothAfter);

        $form->number('user_id', __('User id'));
        $form->number('rent_id', __('Rent id'));
        $form->number('shop_id', __('Shop id'));
        $form->text('commodity_attr_id', __('Commodity attr id'));
        $form->decimal('price', __('Price'))->default(0.00);
        $form->text('comment', __('Comment'));
        $form->switch('status', __('Status'));

        return $form;
    }
}
